package cn.edu.hust.sse.software7.class3.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.hust.sse.software7.class3.common.MD5Tool;
import cn.edu.hust.sse.software7.class3.entities.EBAdmin;
import cn.edu.hust.sse.software7.class3.service.EBAdminService;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBAdminServiceImpl;

public class AdminServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	EBAdminService ebs = new EBAdminServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		String method = req.getParameter("method");
		if (method.equals("delete")) {
			String admin2Delete = req.getParameter("admin2Delete");
			Long admin2DeleteId = new Long(admin2Delete);
			ebs.deleteAdminById(admin2DeleteId);
			List<EBAdmin> admins = ebs.getAdmins();
			session.setAttribute("adminList", admins);
			resp.sendRedirect("admin/adminManage/watchAdmin.jsp");
		}if (method.equals("logout")) {
			session.removeAttribute("admin");
			session.invalidate();
			resp.sendRedirect("admin/index.jsp");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		String method = req.getParameter("method");
		if (method.equals("login")) {
			String adminName = req.getParameter("adminName");
			String adminPass = req.getParameter("adminPass");
			boolean checkInit = ebs.isInited();
			if (!checkInit) {
				EBAdmin admin = new EBAdmin("admin", MD5Tool.Encrypt("admin"),
						"The initial admin.");
				admin.setEnable(true);
				ebs.addAdmin(admin);
				session.setAttribute("admin", admin);
				List<EBAdmin> admins = ebs.getAdmins();
				session.setAttribute("adminList", admins);
				resp.sendRedirect("admin/management.jsp");
			} else {
				boolean checkAdmin = ebs.isExistedAdmin(adminName);
				if (!checkAdmin) {
					session.setAttribute("message", "该用户不存在。");
					resp.sendRedirect("admin/index.jsp");
				} else {
					EBAdmin admin = new EBAdmin(adminName,
							MD5Tool.Encrypt(adminPass));
					if (ebs.validateAdmin(admin)) {
						session.setAttribute("admin", admin);
						List<EBAdmin> admins = ebs.getAdmins();
						session.setAttribute("adminList", admins);
						resp.sendRedirect("admin/management.jsp");
					} else {
						session.setAttribute("message", "用户名或密码错误。");
						resp.sendRedirect("admin/index.jsp");
					}
				}
			}
		} else if (method.equals("addadmin")) {
			session.removeAttribute("AAmessage");
			String adName = req.getParameter("adName");
			String passwd = req.getParameter("passwd");
			String repasswd = req.getParameter("repasswd");
			String description = req.getParameter("description");
			boolean checkPass = passwd.trim().equals(repasswd.trim());
			if(checkPass){
				boolean checkAdmin = ebs.isExistedAdmin(adName);
				if(!checkAdmin){
					EBAdmin admin = new EBAdmin(adName, MD5Tool.Encrypt(passwd));
					admin.setEnable(true);
					admin.setDescription(description);
					ebs.addAdmin(admin);
					session.setAttribute("AAmessage", "添加成功");
				}else{
					session.setAttribute("AAmessage", "该用户已存在！");
				}
			}else{
				session.setAttribute("AAmessage", "密码不一致！");
			}
			resp.sendRedirect("admin/adminManage/addAdmin.jsp");
		} else if (method.equals("updateadmin")) {
			String passwd = req.getParameter("passwd");
			String newpasswd = req.getParameter("newpasswd");
			String repasswd = req.getParameter("repasswd");
			String active = req.getParameter("active");
			String description = req.getParameter("description");
			EBAdmin admin = (EBAdmin) session.getAttribute("admin2Update");
			if (newpasswd.trim().equals(repasswd.trim())) {
				if (MD5Tool.Encrypt(passwd).equals(admin.getPasswd())) {
					if(!newpasswd.trim().equals("")){
					admin.setPasswd(MD5Tool.Encrypt(newpasswd));}
					admin.setDescription(description);
					admin.setEnable(null != active && active.equals("on"));
					ebs.updateAdmin(admin);
					session.setAttribute("admin2Update", admin);
					session.setAttribute("updateMessage", "修改成功！");
				} else {
					session.setAttribute("updateMessage", "密码错误");
				}
			} else {
				session.setAttribute("updateMessage", "新密码不一致");
			}
			resp.sendRedirect("admin/adminManage/updateAdmin.jsp");
		}

	}

}
